package com.lanhao.aot.iot;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

public class DeviceBindAndActiveUnionUtil {

	public static String excelBindFilePath = "D:/device/绑定设备数量.xlsx";
	public static String excelActiveFilePath = "D:/device/激活设备数量.xlsx";
	public static String LINE_SEPARATOR = "\r\n";
	public static Map<String, DeviceStaticData> activeMap = new HashMap<>();
	public static Map<String, DeviceStaticData> bindMap = new HashMap<>();

	public static List<DeviceStaticData> finalOutList = new ArrayList<DeviceStaticData>();

	public static void main(String[] args) {
		try {

			ExcelReader reader2 = ExcelUtil.getReader(new File(excelActiveFilePath), 0);
			List<List<Object>> dataList2 = reader2.read();
			for (int i = 1; i < dataList2.size(); i++) {
				List<Object> list = dataList2.get(i);
				String manufacturer = String.valueOf(list.get(0)).trim();
				String brandName = String.valueOf(list.get(1)).trim();
				String modelName = String.valueOf(list.get(2)).trim();
				String platformlName = String.valueOf(list.get(3)).trim();
				String category = String.valueOf(list.get(4)).trim();
				String bigCategory = String.valueOf(list.get(5)).trim();
				String smallCategory = String.valueOf(list.get(6)).trim();
				String activeNumber = String.valueOf(list.get(7)).trim();
				DeviceStaticData deviceStaticData = new DeviceStaticData(manufacturer, brandName, modelName,
						platformlName, category, bigCategory, smallCategory);
				deviceStaticData.setActiveNumber(activeNumber);

				String key = manufacturer + brandName + modelName + platformlName;

				activeMap.put(key, deviceStaticData);
			}

			ExcelReader reader = ExcelUtil.getReader(new File(excelBindFilePath), 0);
			List<List<Object>> dataList = reader.read();
			for (int i = 1; i < dataList.size(); i++) {
				List<Object> list = dataList.get(i);
				String manufacturer = String.valueOf(list.get(0)).trim();
				String brandName = String.valueOf(list.get(1)).trim();
				String modelName = String.valueOf(list.get(2)).trim();
				String platformlName = String.valueOf(list.get(3)).trim();
				String category = String.valueOf(list.get(4)).trim();
				String bigCategory = String.valueOf(list.get(5)).trim();
				String smallCategory = String.valueOf(list.get(6)).trim();
				String bindNum = String.valueOf(list.get(7)).trim();
				String key = manufacturer + brandName + modelName + platformlName;
				DeviceStaticData deviceStaticData = new DeviceStaticData(manufacturer, brandName, modelName,
						platformlName, category, bigCategory, smallCategory);
				deviceStaticData.setBindNumber(bindNum);
				bindMap.put(key, deviceStaticData);
			}

			activeMap.forEach((k, v) -> {
				// 绑定的包括激活的
				if (bindMap.containsKey(k)) {
					activeMap.get(k).setBindNumber(bindMap.get(k).getBindNumber());
				} else {
					activeMap.get(k).setBindNumber("0");
				}
			});

			bindMap.forEach((k, v) -> {
				// 绑定的包括激活的
				if (activeMap.containsKey(k)) {
					bindMap.get(k).setActiveNumber(activeMap.get(k).getActiveNumber());
				} else {
					bindMap.get(k).setActiveNumber("0");
				}
			});

			Map<String, DeviceStaticData> combineResultMap = new HashMap<String, DeviceStaticData>();
			combineResultMap.putAll(bindMap);
			combineResultMap.putAll(activeMap);

			combineResultMap.forEach((k, v) -> {
				finalOutList.add(v);
			});

			// 通过工具类创建writer
			ExcelWriter writer = ExcelUtil.getWriter("D:/device/设备激活绑定设备信息表.xlsx");
			// 合并单元格后的标题行，使用默认标题样式
			writer.merge(8, "设备激活绑定清单明细");
			// 一次性写出内容，使用默认样式，强制输出标题
			writer.write(finalOutList, true);
			// 关闭writer，释放内存
			writer.close();

//			FileUtil.appendString(sb.toString(), new File(scriptFilePath), Charset.forName("UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
